package com.CST.controller.ys;

import com.CST.common.BaseEntity;
import com.CST.common.api.ResultObject;
import com.CST.dto.ys.YsManageSubjectDTO;
import com.CST.entity.ys.YsManageSubject;
import com.CST.service.ys.YsManageSubjectService;
import com.CST.vo.ys.YsManageSubjectListVO;
import com.CST.vo.ys.YsManageSubjectVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author qtx
 * @since 2023-04-24
 */
@Api(tags = "管理职能科目对照")
@RestController
@RequestMapping("/ysManageSubject")
public class YsManageSubjectController {

    private final YsManageSubjectService service;

    public YsManageSubjectController(YsManageSubjectService service) {
        this.service = service;
    }

    @ApiOperation("分页查询")
    @PostMapping("/listYsManageSubjectPage")
    public ResultObject<IPage<YsManageSubjectVO>> listYsManageSubjectPage(@RequestBody YsManageSubjectDTO dto) {
        return ResultObject.success(service.listYsManageSubjectPage(dto));
    }

    @ApiOperation("新增或编辑")
    @PostMapping("/updateYsManageSubject")
    public ResultObject<Boolean> updateYsManageSubject(@RequestBody YsManageSubject entity) {
        if (entity.getYySubjectCodes() != null){

            for (int i = 0;i<entity.getYySubjectCodes().length;i++) {
                YsManageSubject subject = new YsManageSubject();
                subject.setId(entity.getId());
                subject.setSubjectId(entity.getSubjectId());
                subject.setYyProject(entity.getYyProject());
                subject.setRemark(entity.getRemark());
                subject.setFlag(Boolean.TRUE);
                subject.setYear(entity.getYear());
                subject.setCreateTime(entity.getCreateTime());
                subject.setCreateBy(entity.getCreateBy());
                subject.setYySubjectCode(entity.getYySubjectCodes()[i]);
                subject.setYySubjectName(entity.getYySubjectNames()[i]);
                service.saveOrUpdate(subject);
            }
            // 先判断，新增的预算科目id与用友id是否在表中存在，如果存在，不允许添加
            Integer count = service.selectByIds(entity);
            if (count > entity.getYySubjectCodes().length){
                return ResultObject.failed("预算科目与用友科目以及存在，请不要重复添加");
            }
        }
        return ResultObject.success(true,"操作成功");
    }

    @ApiOperation("删除")
    @DeleteMapping("/removeYsManageSubject")
    public ResultObject<Boolean> removeYsManageSubject(@RequestParam Long id) {
        return ResultObject.success(service.update(Wrappers.lambdaUpdate(YsManageSubject.class)
                .set(BaseEntity::getDeleteFlag, Boolean.TRUE)
                .eq(BaseEntity::getId, id)));
    }

    @ApiOperation("继承")
    @GetMapping("/inherit")
    public ResultObject<Boolean> inheritManage(@RequestParam Integer source, @RequestParam Integer target) {
        return ResultObject.success(service.inherit(source, target));
    }

    @ApiOperation("导出")
    @PostMapping("/exportExcelYsManageSubject")
    public void exportExcelYsManageSubject(HttpServletResponse response, @RequestBody YsManageSubjectDTO dto) {
        service.exportExcelYsManageSubject(response, dto);
    }

    @ApiOperation("导出模板")
    @PostMapping("/exportExcelYsManageSubjectDemo")
    public void exportExcelYsManageSubjectDemo(HttpServletResponse response) {
        service.exportExcelYsManageSubjectDemo(response);
    }

    @ApiOperation("导入")
    @PostMapping("/importExcelYsManageSubject")
    public void importExcelYsManageSubject(MultipartFile file) {
        service.importExcelYsManageSubject(file);
    }

    @ApiOperation("通过预算科目id查询")
    @GetMapping("/listYsManageSubject")
    public ResultObject<List<YsManageSubjectListVO>> listYsManageSubject(@RequestParam Long subjectId, String name) {
        return ResultObject.success(service.listYsManageSubject(subjectId, name));
    }
}
